package jp.co.hcs.attendancemanagement.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jp.co.hcs.attendancemanagement.AMConstants;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * The Class AMAction.
 */
public abstract class AMAction extends Action {

  /** The Constant LOGIN. */
  protected static final String LOGIN = "login";

  /** The Constant SUCCESS. */
  protected static final String SUCCESS = "success";

  /** The Constant INVALID_TOKEN. */
  protected static final String INVALID_TOKEN = "invalid_token";

  /** The Constant INVALID_SESSION. */
  protected static final String INVALID_SESSION = "invalidSession";

  public final ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    HttpSession session = request.getSession(false);
    if (session == null || session.isNew()) {
      return mapping.findForward(INVALID_SESSION);
    }

    ActionForward forward = null;
    if (request.getSession().getAttribute(AMConstants.USER_LOGGED_IN) != null) {
      forward = executeImpl(mapping, form, request, response);
    } else {
      forward = mapping.findForward(LOGIN);
    }
    return forward;
  }

  /**
   * Execute impl.
   * 
   * @param mapping the mapping
   * @param form the form
   * @param request the request
   * @param response the response
   * @return the action forward
   * @throws Exception the exception
   */
  public abstract ActionForward executeImpl(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception;
}
